package cn.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ExceResult;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.sys.MenuEntity;
import cn.kpi.entity.vtiondpline.VtDpItemEntity;
import cn.kpi.service.vtiondpline.IVtDpItemService;

@Controller
@RequestMapping("vtdpset")
public class VtDpItemController extends BaseController {
	@Autowired
	private IVtDpItemService iVtDpItemService;
	
	@RequestMapping(value = "/vtdp_list")
	public String VtDpList(Model model) {
		return "vtiondpline/vtdp_list";
	}
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public LayTable<VtDpItemEntity> GetList(HttpServletRequest request) {
		List<VtDpItemEntity> lst=null;
		LayTable<VtDpItemEntity> layTable=new LayTable<VtDpItemEntity>();
		try {
			String vtdpname = request.getParameter("vtdpname");
			String djxg = request.getParameter("djxg");
			String kpxg = request.getParameter("kpxg");
			String gzxg = request.getParameter("gzxg");
			String where="";
			if(vtdpname!=null&&vtdpname.length()>0){
				where+=" AND vtdpname LIKE '%"+vtdpname+"%'";
			}
			if(djxg!=null&&djxg.length()>0){
				where+=" AND djxg="+djxg;
			}
			if(kpxg!=null&&kpxg.length()>0){
				where+=" AND kpxg="+kpxg;
			}
			if(gzxg!=null&&gzxg.length()>0){
				where+=" AND gzxg="+gzxg;
			}
			
			lst = iVtDpItemService.GetVtDpItemListByWhere(where);
			

			layTable.setCode(0);
			layTable.setCount(100);
			layTable.setData(lst);
			
		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	@RequestMapping(value = "/add_vtdpitem")
	public String AddVtDpItem(HttpServletRequest request,HttpServletResponse response){
		try{
			VtDpItemEntity entity=new VtDpItemEntity();
			//entity.setDjpdxg(Integer.parseInt(request.getParameter("djpdxg")));
			entity.setVtdpid(Long.parseLong(request.getParameter("vtdpid")));
//			entity.setDjxg(Integer.parseInt(request.getParameter("djxg")));
			entity.setRemark(request.getParameter("remark"));
//			entity.setGzxg(Integer.parseInt(request.getParameter("gzxg")));
			//entity.setKppdxg(Integer.parseInt(request.getParameter("kppdxg")));
			//entity.setKptype(Integer.parseInt(request.getParameter("kptype")));
			//entity.setKpxg(Integer.parseInt(request.getParameter("kpxg")));
			entity.setQxdw(request.getParameter("qxdw"));
			entity.setQxnum(Integer.parseInt(request.getParameter("qxnum")));
			entity.setVtdpdes(request.getParameter("vtdpdes"));
			entity.setVtdpname(request.getParameter("vtdpname"));
			
			entity.setMzpdcllxallow(0);
			entity.setMzpdcllx(request.getParameter("mzpdcllx"));
			entity.setZzpdcllxallow(0);
			entity.setZzpdcllx(request.getParameter("zzpdcllx"));
			
			int iResult=0;
			if(entity.getVtdpid()>0){
				iResult=iVtDpItemService.UpdateEntity(entity);	
			}
			else{
				iResult=iVtDpItemService.InsertEntity(entity);	
			}
			
			
			return renderStringScript(response,"<script>window.parent.CloseDialog("+iResult+")</script>");
		}
		catch(Exception ex){
			ex.printStackTrace();
			return renderStringScript(response,"<script>window.parent.CloseDialog(0)</script>");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete_vtdpitem")
	public ExceResult DeleteVtDpItem(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long vtdpid=Long.parseLong(request.getParameter("vtdpid"));
		 int iResult=iVtDpItemService.DeleteEntity(vtdpid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
}
